package com.yuke.cloud.service.omc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.HttpAesUtil;
import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.uac.dto.UserInfoDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.omc.dto.OdsPointsDto;
import com.yuke.cloud.service.omc.entity.OdsPoints;
import com.yuke.cloud.service.omc.entity.OdsUserPoint;
import com.yuke.cloud.service.omc.service.OdsPointsService;
import com.yuke.cloud.service.omc.service.OdsUserPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* Created by zn on 2019/12/25.
*/
@Api(value = "WEB - OdsPointsController", tags = "处理OdsPoints相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/ods/points")
public class OdsPointsController extends BaseController {
    @Resource
    private OdsPointsService odsPointsService;
    @Resource
    private OdsUserPointService odsUserPointService;
    @Resource
    private UacUserFeignApi uacUserFeignApi;


    @PostMapping("/save")
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "odsPoints", value = "该数据记录的POJO") @RequestBody OdsPoints odsPoints) {
        OdsPointsDto odsPointsDto = new OdsPointsDto();
        BeanUtils.copyProperties(odsPoints,odsPointsDto);
        this.addSignIn(odsPointsDto);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        odsPointsService.deleteById(id);
        return WrapMapper.ok();
    }

    @PostMapping("/update")
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "odsPoints", value = "需要修改的内容") @RequestBody OdsPoints odsPoints) {
        odsPointsService.update(odsPoints);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<OdsPoints> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        OdsPoints odsPoints = odsPointsService.findById(id);
        return WrapMapper.ok(odsPoints);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<OdsPoints>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<OdsPoints> list = odsPointsService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/searchPoints")
    @ApiOperation(httpMethod = "POST", value = "获取用户名")
    public Wrapper findPoints(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                              @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                              @RequestBody(required = false)OdsPointsDto odsPointsDto){
        PageHelper.startPage(page, size);
        List<OdsPointsDto> points = odsPointsService.findPoints(odsPointsDto);
        PageInfo pageInfo = new PageInfo(points);
        return WrapMapper.ok(pageInfo);
    }




    @PostMapping("/addSignIn")
    @ApiOperation(httpMethod = "POST", value = "签到积分，新增一条记录")
    public Wrapper addSignIn(@ApiParam(name = "odsPoints", value = "该数据记录的POJO") @RequestBody OdsPointsDto odsPointsDto) {
        if (odsPointsDto != null && PublicUtil.isNotEmpty(odsPointsDto.getNumber()) && odsPointsDto.getNumber()>0){

            if (odsPointsDto.getPointType().equals((byte)2)){
                Calendar cal = Calendar.getInstance();
                cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
                Date beginOfDate = cal.getTime();

                odsPointsDto.setCreateTime(beginOfDate);
                List<OdsPointsDto> aa = odsPointsService.findPoints(odsPointsDto);
                if (aa != null && aa.size() > 0){
                   return WrapMapper.error("今日已经签到！");
               }
            }


            //查找ods_point取出最新point_balance值
            OdsPoints odsPoints = new OdsPoints();
            odsPoints = odsPointsService.selectLastOneByUserId(odsPointsDto.getUserId());
            if (odsPoints != null ){
                odsPointsDto.setPointBalance(odsPoints.getPointBalance());
            }else{
                odsPointsDto.setPointBalance(0);
            }


            OdsUserPoint odsUserPoint = new OdsUserPoint();
            odsUserPoint.setUserId(odsPointsDto.getUserId());
            Integer num = odsUserPointService.selectCount(odsUserPoint);

            //保存积分余额表
            if (num < 1){
                if (odsPointsDto.getLoginName() == null){
                    Wrapper wrapper = uacUserFeignApi.getUserInfoByUserId(odsPointsDto.getUserId());
                    if (PublicUtil.isNotEmpty(wrapper) && PublicUtil.isNotEmpty(wrapper.getResult())){
                        UserInfoDto userInfoDto = (UserInfoDto)wrapper.getResult();
                        odsPointsDto.setLoginName(userInfoDto.getLoginName());
                    }else{
                        return WrapMapper.error("积分录入错误");
                    }
                }
                odsUserPoint.setLoginName(odsPointsDto.getLoginName());
                odsUserPoint.setUserPointId(generateId());
                odsUserPoint.setPointBalance(HttpAesUtil.encode("0"));
                odsUserPointService.save(odsUserPoint);
            }

            odsPointsDto.setPointId(generateId());
            odsPointsDto.setPointStatus((byte)0);


            Long userId = odsPointsService.doPointUpdate(odsPointsDto);

            if (userId>0){
                return WrapMapper.ok("success");
            }
        }

        return WrapMapper.error("积分录入错误");
    }

    @PostMapping("/getSignInInfo")
    @ApiOperation(httpMethod = "POST", value = "获取签到信息")
    public Wrapper getSignInInfo(@RequestBody(required = false) OdsPointsDto odsPointsDto){
        if(odsPointsDto == null || odsPointsDto.getUserId() == null){
            WrapMapper.error("传入参数错误！");
        }
        Map<String,Object> map =  odsPointsService.getSignInInfo(odsPointsDto);

        return WrapMapper.ok(map);
    }

    @GetMapping("/getLastPoint/{userId}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<OdsPoints> selectLastOneByUserId(@ApiParam(name = "id", value = "记录id") @PathVariable Long userId) {
        return WrapMapper.ok(odsPointsService.selectLastOneByUserId(userId));
    }
}
